<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;销售管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-order">&nbsp;&nbsp;销售订单管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
      <el-form ref="form" :model="salesForm">
        <el-form-item class="sales_formSubmit_search">
          <el-select v-model="salesForm.customName" placeholder="请选择销售订单名称"
                     @change="loadSalesList()">
            <el-option
                v-for="item in categoryOptions"
                :key="item.id"
                :label="item.customName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="sales.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="orderDate" label="下单日期" width="200px"></el-table-column>
        <el-table-column prop="orderId" label="订单编号" width="200px"></el-table-column>
        <el-table-column prop="category" label="商品" width="200px"></el-table-column>
        <el-table-column prop="unit" label="单位" width="200px"></el-table-column>
        <el-table-column prop="count" label="数量" width="200px"></el-table-column>
        <el-table-column prop="unitPrice" label="单价" width="200px"></el-table-column>
        <el-table-column prop="amount" label="金额" width="200px"></el-table-column>
        <el-table-column prop="customName" label="客户姓名" width="200px"></el-table-column>
        <el-table-column prop="cell" label="电话号码" width="200px"></el-table-column>
        <el-table-column prop="deliveryDate" label="送货日期" width="200px"></el-table-column>
        <el-table-column prop="deliveryOdd" label="送货编号" width="200px"></el-table-column>
        <el-table-column prop="deliveryCheck" label="是否收到" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="sales.length">
        </el-pagination>
      </div>
      <!-- 以下是弹出的修改固定资产的对话框 -->
      <el-dialog
          width="40%"
          :modal-append-to-body="false"
          title="编辑订单"
          :visible.sync="dialogFormVisible">
        <el-form :model="salesForm">
          <el-form-item label="下单日期" :label-width="formLabelWidth">
            <el-date-picker
                v-model="salesForm.orderDate"
                type="date"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                placeholder="选择入职时间">
            </el-date-picker>
          </el-form-item>
          <el-row>
            <el-col style="width: 400px">
              <el-form-item label="订单编号" :label-width="formLabelWidth">
                <el-input v-model="salesForm.orderId" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col style="width: 700px">
              <el-form-item label="商品" :label-width="formLabelWidth">
                <el-input v-model="salesForm.category" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="单位" :label-width="formLabelWidth" style="width: 700px">
            <el-input v-model="salesForm.unit" autocomplete="off"></el-input>
          </el-form-item>
          <el-row>
            <el-col style="width: 200px;">
              <el-form-item label="数量" :label-width="formLabelWidth">
                <el-input v-model="salesForm.count" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col style="width: 240px;">
              <el-form-item label="单价" :label-width="formLabelWidth">
                <el-input v-model="salesForm.unitPrice" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col style="width: 240px;">
              <el-form-item label="金额" :label-width="formLabelWidth">
                <el-input v-model="salesForm.amount" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col style="width: 300px;">
              <el-form-item label="客户姓名" :label-width="formLabelWidth">
                <el-input v-model="salesForm.customName" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col style="width: 400px;">
              <el-form-item label="电话号码" :label-width="formLabelWidth">
                <el-input v-model="salesForm.cell" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col style="width: 300px;">
              <el-form-item label="送货日期" :label-width="formLabelWidth">
                <el-date-picker
                    v-model="salesForm.deliveryDate"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择入职时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col style="width: 400px;">
              <el-form-item label="送货编号" :label-width="formLabelWidth">
                <el-input v-model="salesForm.deliveryOdd" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
            <el-col style="width: 200px;">
              <el-form-item label="是否收到" :label-width="formLabelWidth">
                <el-input v-model="salesForm.deliveryCheck" autocomplete="off"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" type="warning" style="color:white;">取 消</el-button>
          <el-button type="primary" @click="submitEditForm()" style="color:white;">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: '120px',
      categoryOptions: [],
      sales: [],
      salesForm: {
        id: '',
        orderDate: '',
        orderId: '',
        category: '',
        unit: '',
        count: '',
        unitPrice: '',
        amount: '',
        customName: '',
        cell: '',
        deliveryDate: '',
        deliveryOdd: '',
        deliveryCheck: '',
      },
      currentPage: 1,
      pageSize: 10,
    }
  },
  methods: {
    /*加载搜索固定资产名*/
    loadSalesList() {
      let url = 'http://localhost:9080/customSales';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.sales = responseBody.data;
          this.categoryOptions = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openEditDialog(sales) {
      let url = 'http://localhost:9080/customSales/' + sales.id;
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.salesForm = responseBody.data;
          console.log('data = ' + responseBody.data);
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadSalesList();
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9080/customSales/' + this.salesForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.salesForm);
      console.log('formData = ' + formData);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改订单成功！',
            type: 'success'
          });
          this.loadSalesList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadSalesList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    /*表单删除*/
    openDeleteConfirm(sales) {
      let title = '提示';
      let message = '此操作将永久删除【' + sales.customName + '】属性, 是否继续?';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(sales);
      }).catch(() => {
      });
    },
    handleDelete(sales) {
      let url = 'http://localhost:9080/customSales' + sales.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除属性【' + sales.customName + '】成功！',
            type: 'success'
          });
          this.loadSalesList();
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadSalesList();
            }
          });
        }
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadSalesList();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.sales_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>